package study.chapter03.stream;

import java.io.*;

public class TestInputStream {
    public static void main(String[] args) throws IOException {
        //input();
        input2();
    }

    public static void input() throws IOException {
        File file;
        InputStream is = null;
        try {
            //方式1
            //InputStream is = new FileInputStream("F:/0temp/hello.txt");

            //方式2
            file = new File("F:/0temp/hello.txt");
            is = new FileInputStream(file);

            int size = is.available();
            for (int i = 0; i < size; i++) {
                char read = (char) is.read();
                System.out.print(read);
            }
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            is.close();
        }
    }

    public static void input2() throws IOException {
        File file;
        InputStream is = null;
        InputStreamReader reader = null;
        try {
            file = new File("F:/0temp/hello.txt");
            is = new FileInputStream(file);
            reader = new InputStreamReader(is, "UTF-8");
            StringBuffer buffer = new StringBuffer();
            while (reader.ready()){
                buffer.append((char)is.read());
            }
            System.out.println(buffer.toString());
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            reader.close();
            is.close();
        }
    }
}
